package com.numberone.web.controller.order;

import com.numberone.framework.web.base.BaseController;
import com.numberone.system.domain.Goods;
import com.numberone.system.domain.GoodsDetail;
import com.numberone.system.domain.XyOrder;
import com.numberone.system.domain.XyUser;
import com.numberone.system.service.*;
import com.numberone.system.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/api/XyOrder")
public class XyOrderApiController extends BaseController {

    @Autowired
    private IXyOrderService xyOrderService;
    @Autowired
    private IGoodsDetailService goodsDetailService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IShopService shopService;
    @Autowired
    private IXyUserService xyUserService;

    /**
     * 获取订单号
     * @param userId
     * @return
     */
    @ResponseBody
    @PostMapping("/getOrderNumber")
    public Object orderNumber(Long userId)
    {
        String orderCode = new OrderCodeFactory().getOrderCode(userId);
        return JsonResult.success(orderCode);
    }

    /**
     * 添加订单
     * @param orderList
     * @return
     */
    @ResponseBody
    @PostMapping("/addOrder")
    @Transactional(propagation = Propagation.REQUIRED)
    public Object addOrder(@RequestBody List<XyOrder> orderList)
    {
        System.out.println(orderList.size()+"-------");
        XyOrder xyOrder = new XyOrder();
        XyOrder xyOrderById = new XyOrder();
        List<XyOrder> list = new ArrayList<>();
        for (XyOrder addOrder : orderList)
        {
            xyOrder.setOrderNumber(addOrder.getOrderNumber());
            xyOrder.setGoodsDetailId(addOrder.getGoodsDetailId());
            xyOrder.setQuantity(addOrder.getQuantity());
            xyOrder.setUserId(addOrder.getUserId());
            xyOrder.setFare(addOrder.getFare());
            xyOrder.setStartTime(new Date());
            xyOrder.setShopId(addOrder.getShopId());
            GoodsDetail goodsDetail = goodsDetailService.selectGoodsDetailById(addOrder.getGoodsDetailId());
            xyOrder.setPrice(goodsDetail.getPrice());
            xyOrder.setOrderPrice(xyOrder.getPrice());
            xyOrder.setStatus(0);
            xyOrderService.insertXyOrder(xyOrder);
            Long order = xyOrder.getId();
            System.out.println(order+"=======");
            if (order != 0)
            {
                GoodsDetail detail = goodsDetailService.selectGoodsDetailById(addOrder.getGoodsDetailId());
                detail.setStock(detail.getStock() - addOrder.getQuantity());
                GoodsDetail newDetail = new GoodsDetail();
                newDetail.setId(detail.getId());
                newDetail.setStock(detail.getStock());
                goodsDetailService.updateGoodsDetail(newDetail);
//                return JsonResult.success();
            }
            XyOrder xyOrder1 = xyOrderService.selectXyOrderById(order);
            System.out.println(xyOrder1.getOrderNumber()+"-------");
            list.add(xyOrderService.selectXyOrderById(order));
        }
        xyOrderById.setOrderList(list);
        if (list != null)
        {
            return JsonResult.success(xyOrderById);
        }else {
            return JsonResult.defaultError();
        }
//        int i = 1;
//        Long userId = (long)i;

//        if (list.size() > 1)
//        {
//            int i = 1;
//            Long userId = (long)i;
//            String orderCode = new OrderCodeFactory().getOrderCode(userId);
//            for (XyOrder x : list)
//            {
//                XyOrder xyOrder = new XyOrder();
//                xyOrder.setOrderNumber(orderCode);
//                xyOrder.setGoodsDetailId(x.getGoodsDetailId());
//                xyOrder.setQuantity(x.getQuantity());
//                xyOrder.setStartTime(new Date());
//                xyOrder.setShopId(x.getShopId());
//                GoodsDetail goodsDetail = goodsDetailService.selectGoodsDetailById(x.getGoodsDetailId());
//                xyOrder.setPrice(goodsDetail.getPrice());
//                xyOrder.setOrderPrice(xyOrder.getPrice());
//                xyOrder.setStatus(1);
//                int order = xyOrderService.insertXyOrder(xyOrder);
//                if (order == 0)
//                {
//                    return JsonResult.defaultError();
//                }
//            }
//            return JsonResult.success();
//        }else {
//            XyOrder xyOrder1 = list.get(0);
//            int i = 1;
//            Long userId = (long)i;
//            XyOrder xyOrder = new XyOrder();
//            xyOrder.setOrderNumber(orderCode);
//            xyOrder.setGoodsDetailId(xyOrder1.getGoodsDetailId());
//            xyOrder.setQuantity(xyOrder1.getQuantity());
//            xyOrder.setStartTime(new Date());
//            xyOrder.setShopId(xyOrder1.getShopId());
//            GoodsDetail goodsDetail = goodsDetailService.selectGoodsDetailById(xyOrder1.getGoodsDetailId());
//            xyOrder.setPrice(goodsDetail.getPrice());
//            xyOrder.setOrderPrice(xyOrder.getPrice());
//            xyOrder.setStatus(1);
//            int order = xyOrderService.insertXyOrder(xyOrder);
//            if (order > 0)
//            {
//                return JsonResult.success();
//            }else {
//                return JsonResult.defaultError();
//            }
//        }

    }

    /**
     * 获取用户订单列表
     * @param userId
     * @return
     */
    @ResponseBody
    @PostMapping("/getOrderList")
    public Object getOrderList(Long userId)
    {
        XyOrder xyOrder = new XyOrder();
        xyOrder.setUserId(userId);
        List<XyOrder> xyOrders = xyOrderService.selectXyOrderList(xyOrder);
        for (XyOrder x : xyOrders)
        {
            List<XyOrder> xyOrdersList = xyOrderService.selectXyOrderByOrderNum(x.getOrderNumber());
            x.setOrderList(xyOrdersList);
        }
        if (xyOrders == null)
        {
            return JsonResult.nodata();
        }
        return JsonResult.success(xyOrders);
    }

    /**
     * 根据状态获取用户订单列表
     * @param userId
     * @param status
     * @return
     */
    @ResponseBody
    @PostMapping("/getOrderListByStatu")
    public Object getOrderListByStatu(Long userId,Integer status)
    {
        XyOrder xyOrder = new XyOrder();
        xyOrder.setUserId(userId);
        xyOrder.setStatus(status);
        List<XyOrder> xyOrders = xyOrderService.selectXyOrderList(xyOrder);
        if (xyOrders == null)
        {
            return JsonResult.nodata();
        }
        return JsonResult.success(xyOrders);
    }

    /**
     * 确认收货
     * @param orderId
     * @param userId
     * @return
     */
    @ResponseBody
    @PostMapping("/receipt")
    public Object receipt(Long orderId, Long userId)
    {
        if (userId == null)
        {
            return JsonResult.noLogin();
        }
        XyOrder order = xyOrderService.selectXyOrderById(orderId);
        if (order == null)
        {
            return JsonResult.nodata();
        }
        XyOrder xyOrder = new XyOrder();
        xyOrder.setUserId(userId);
        xyOrder.setId(orderId);
        XyOrder orderById = xyOrderService.selectXyOrderById(orderId);
        if (orderById.getStatus() == 3)
        {
            return JsonResult.error(208,"订单已确认",null);
        }
        //修改订单状态
        xyOrder.setStatus(3);
        xyOrderService.updateXyOrder(xyOrder);
        //修改用户欣亿积分
        XyUser xyUser = xyUserService.selectXyUserById(userId);
        GoodsDetail goodsDetail = goodsDetailService.selectGoodsDetailById(order.getGoodsDetailId());
        Goods goods = goodsService.selectGoodsById(goodsDetail.getGoodsId());
        xyUser.setXinyiIntegral(xyUser.getXinyiIntegral() + goods.getGoodIntegral());
        int i = xyUserService.updateXyUser(xyUser);
        if (i > 0)
        {
            return JsonResult.success();
        }
        return JsonResult.defaultError();
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @ResponseBody
    @PostMapping("/deleteOrder")
    public Object deleteOrder(Long orderId)
    {
        int i = xyOrderService.deleteXyOrderById(orderId);
        if (i > 0)
        {
            return JsonResult.success();
        }
        return JsonResult.nodata();
    }


}
